package com.spr.gw.interceptor;

import com.spr.gw.common.EnumCode;
import com.spr.gw.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by lsy
 * 登录拦截器
 * on 2017/10/22 12:02.
 */
public class LoginInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String url = request.getRequestURI();
        if(!url.startsWith("/admin")){
            return true;
        }
        if("/admin/login.html".equals(url)||"/admin/loginSubmit".equals(url)){
            return true;
        }
        User user = (User) request.getSession().getAttribute(EnumCode.LOGIN_USER.getValue());
        if(user == null){
            request.getRequestDispatcher("/admin/login.html").forward(request,response);
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
